/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ferrocarril.util;

import java.util.HashSet;
import java.util.Set;

/**
 * Tomado de:
 * @OriginalAuthor http://manuel.midoriparadise.com/2012/03/obteniendo-todas-las-posibles-palabras-de-7-letras-o-menos/
 * 
 * Existen modificaciones sobre el codifo original para efectos del presente ejercicio.
 */
public class Shuffle {
    public static Set<String> palabras = new HashSet<String>();

    public static Set<String> shuffle(String word) {
        Set<String> words = new HashSet<String>();
        Set<String> newWords = new HashSet<String>();
        String newWord = "";
        String curChar = "";
        String revWord = "";
        String checked = "";
        
        switch (word.length()) {
            case 0:
                break;
            case 1:
                newWords.add(word);
                palabras.addAll(newWords);
                break;
            case 2:
                StringBuilder sb = new StringBuilder(word);
                revWord = sb.reverse().toString();
                newWords.add(word);
                newWords.add(revWord);
                palabras.addAll(newWords);
                break;
            default:
                newWords.add(word);
                for (int i = 0; i < word.length(); i++) {
                    curChar = word.substring(i, i + 1);
                    newWord = "";
                    words.clear();

                    newWord = checked + word.substring(i + 1);
                    if (newWord.length() >= 2) {
                        palabras.addAll(newWords);
                        words.addAll(shuffle(newWord));
                        if (!words.isEmpty()) {
                            for (String w : words) {
                                newWords.add(curChar + w);
                            }
                        }
                    }
                    checked += curChar;
                }
        }

        return newWords;
    }
}
